//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core.Serialization;
using OpenLS.Drawing.Serialization;

namespace OpenLS.Drawing
{
    public class Transform2D : IOfficeXmlSerializable
    {
        private PositiveSize2D chExt;

        private Point2D chOff;
        private bool flipH;
        private bool flipV;
        private Point2D point2D;


        private PositiveSize2D positiveSize2D;
        private double rot;

        public PositiveSize2D ChildSize
        {
            get { return chExt; }
            set { chExt = value; }
        }

        public Point2D ChildOffset
        {
            get { return chOff; }
            set { chOff = value; }
        }

        public PositiveSize2D Size
        {
            get
            {
                if (positiveSize2D == null)
                    positiveSize2D = new PositiveSize2D();
                return positiveSize2D;
            }
            set { positiveSize2D = value; }
        }


        public Point2D Offset
        {
            get
            {
                if (point2D == null)
                    point2D = new Point2D();
                return point2D;
            }
            set { point2D = value; }
        }


        public double RotationAngle
        {
            get { return rot; }
            set { rot = value; }
        }

        public bool FlipH
        {
            get { return flipH; }
            set { flipH = value; }
        }

        public bool FlipV
        {
            get { return flipV; }
            set { flipV = value; }
        }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("xfrm"))
            {
                RotationAngle = c.GetOptionalInteger("rot", 0)/60000d;
                FlipH = c.GetOptionalBoolean("flipH", false);
                FlipV = c.GetOptionalBoolean("flipV", false);
                Offset = c.ReadOptionalElementAs<Point2D>("off");
                Size = c.ReadOptionalElementAs<PositiveSize2D>("ext");
                ChildOffset = c.ReadOptionalElementAs<Point2D>("chOff");
                ChildSize = c.ReadOptionalElementAs<PositiveSize2D>("chExt");
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.WriteAndSwitch("xfrm", DrawingConstants.DrawingNamespace))
            {
                c.SetOptionalInteger("rot", (int) Math.Round(RotationAngle*60000), 0);
                c.SetOptionalBoolean("flipH", FlipH, false);
                c.SetOptionalBoolean("flipV", FlipV, false);
                c.WriteOptionalElementAs(Offset, "off");
                c.WriteOptionalElementAs(Size, "ext");
                c.WriteOptionalElementAs(ChildOffset, "chOff");
                c.WriteOptionalElementAs(ChildSize, "chExt");
            }
        }

        #endregion
    }
}